pub mod user_controller;
pub mod login_controller;
use actix_web::{web, HttpResponse};
use serde_json::json;
use crate::middlewares::auth_middleware::Auth;
use user_controller::config_user_controller;
use login_controller::config_login_controller;
use actix_session::Session;

use crate::error::AppResult;
async fn session_test(session:Session) -> AppResult<HttpResponse> {
    let t = session.get("id")?.unwrap_or(0)+1;
    // session.s("id", t)?;
    session.set("id", t)?;
    Ok(HttpResponse::Ok().json(json!({
        "t":t
    })))
}


pub fn config_controller(cfg: &mut web::ServiceConfig) {
    cfg.service(
        web::resource("/test")
            .route(web::get().to(|| HttpResponse::Ok().json(json!({"success":1,"message":"ok"})))),
    )
    .service(web::resource("/test/session").wrap(Auth::default()).route(web::get().to(session_test)))
    .service(web::scope("/user").configure(config_user_controller))
    .service(web::scope("/login").configure(config_login_controller));
}
